package com.ttmusic.utils;

import java.io.UnsupportedEncodingException;

public class CharsetUtil {
	
	/**
	 * 处理歌曲名称和艺术家名称乱码
	 * @param value
	 * @param code
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static boolean isEncoding(String value, String charset) throws UnsupportedEncodingException {  
        return value.equals(new String(value.getBytes(charset), charset));  
    }
	
	/**
	 * 将目标字符串转为utf-8编码
	 * @param value
	 */
	public static String handleStringTag(String value) {  
        try{  
        	if(isEncoding(value, "UTF-8")) {
            	System.out.println(value + " 是UTF-8格式");
            	//value = new String(value.getBytes("UTF-8"), "GBK");
            }else if(isEncoding(value, "GB2312")) {
            	System.out.println(value + " 是GB2312格式");
            	value = new String(value.getBytes("GB2312"), "utf-8");
            }else if(isEncoding(value, "GB18300")) {
            	System.out.println(value + " 是GB18300格式");
            	value = new String(value.getBytes("GB18300"), "utf-8");
            }else if(isEncoding(value,"ISO-8859-1")){ 
            	System.out.println(value + " 是ISO-8859-1格式");
        		value = new String(value.getBytes("ISO-8859-1"), "utf-8");  
            }
        }catch(UnsupportedEncodingException e){  
            e.printStackTrace();  
        }
        return value;
	}

}
